/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-1
 * @Time  : 下午3:21
 * @brief :
 */

P.createPage({
    _data:{
        type:0,
        date_type:0,
        trade_type:{
            1:'充值',
            2:'提现',
            3:'购买',
            4:'赎回'
        },
        trade_status:{
            s1_0:'等待支付',
            s1_1:'充值成功',
            s1_2:'充值失败',
            s2_0:'等待支付',
            s2_1:'提现成功',
            s2_2:'退回',
            s2_3:'等待支付',
            s2_4:'退回',
            s3_0:'购买中',
            s3_1:'购买成功',
            s3_2:'购买失败',
            s4_0:'赎回中',
            s4_1:'赎回成功',
            s4_2:'赎回失败'
        },
        status_color_type:{
            s1_0:'',
            s1_1:'',
            s1_2:'color2',
            s2_0:'',
            s2_1:'',
            s2_2:'color2',
            s2_3:'',
            s2_4:'color2',
            s3_0:'',
            s3_1:'',
            s3_2:'color2',
            s4_0:'',
            s4_1:'',
            s4_2:'color2'
        }
    },

    _create:function(){
        var Pagination = require('common:widget/ui/pagination/pagination.js').Pagination,
            util = require('common:widget/ui/libs/util.js').util,
            $page = $('.page-myrecord'),
            $recordBlock = $page.find('.record-block'),
            $recordContainer = $recordBlock.find('.record-table tbody'),
            me = this,
            pagination;


        //初始化页码
        pagination = new Pagination({
            container:$recordBlock.find('.record-pagination'),
            perCount:10,
            cur:1,
            onRefresh:function(curPage,callback){
//                $recordContainer.html('');
                $.getJSON('/user/getusertradelog',{
                    'ps':10,
                    'pn':curPage,
                    'type':me.data('type'),
                    'date_type':me.data('date_type')
                },function(ret,status){
                    if(ret.error_no == 0 && ret.data){
                        callback(ret.data.count);
                        me._createTableList(ret.data.list);
                    }
                });
            }
        });


        this.data({
            '$page':$page,
            '$recordBlock':$recordBlock,
            '$timeTab':$recordBlock.find('.time-tab a'),
            '$tradeTab':$recordBlock.find('.trade-tab a'),
            '$recordContainer':$recordContainer,
            'pagination':pagination,
            'util':util
        })
    },


    _init:function(){
        var $timeTab = this.data('$timeTab'),
            $tradeTab = this.data('$tradeTab'),
            pagination = this.data('pagination'),
            $recordContainer = this.data('$recordContainer'),
            me = this;

        $timeTab.click(function(){
            $timeTab.removeClass('active');
            $(this).addClass('active');
            me.data('date_type',$(this).attr('data-date-type'));
            pagination.initPage(1);
//          $recordContainer.html('');
        });

        $tradeTab.click(function(){
            $tradeTab.removeClass('active');
            $(this).addClass('active');
            me.data('type',$(this).attr('data-trade-type'));
            pagination.initPage(1);
//          $recordContainer.html('');
        });
    },

    //购买列表
    _createTableList:function(list){
        var status = this.data('status'),
            util = this.data('util'),
            tradeStatus = this.data('trade_status'),
            tradeType = this.data('trade_type'),
            colorType = this.data('color_type'),
            statusColorType = this.data('status_color_type');
        this.data('$recordContainer').html((function(){
            var html = "";
            $.each(list,function(k,v){
                var pid = null;
                if(!!v.inflow && v.inflow!=100 && v.inflow!=0){
                    pid = v.inflow;
                }else if(!!v.outflow && v.outflow!=100 && v.outflow!=0){
                    pid = v.outflow;
                }
                html+='' +
                '<tr>'+
                '<td>'+ v.serial_id+'</td>' +
                '<td>'+ util.toTime(v.time*1000,'yyyy-MM-dd hh:mm:ss')+'</td>' +
                '<td>'+(pid!=999?tradeType[v.type]:'')+(pid?P.productMap[pid]:'')+'</td>' +
                '<td>￥'+ mathUtil.formatAmount(v.amount)+'</td>' +
                '<td>￥'+ mathUtil.formatAmount(v.fee)+'</td>' +
                '<td class="'+statusColorType['s'+v.type+'_'+v.status]+'">'+ tradeStatus['s'+v.type+'_'+v.status] +'</td>' +
                '<td>'+ ((v.usable_amount==-1)?'-':('￥'+mathUtil.formatAmount(v.usable_amount))) +'</td>' +
                '</tr>';
            });
            return html;
        })());
    }

});